package com.resource.homeworkmanage.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.resource.common.utils.PageUtils;
import com.resource.common.utils.R;
import com.resource.homeworkmanage.entity.TWorkInformationEntity;
import com.resource.homeworkmanage.service.TWorkInformationDetailService;
import com.resource.homeworkmanage.service.TWorkInformationService;
import com.resource.studentmanage.feign.StudentDealRemote;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 作业信息表
 *
 * @author zkx
 * @email 1486664603@qq.com
 * @date 2023-03-06 20:53:55
 */
@RestController
@RequestMapping("work/tworkinformation")
public class TWorkInformationController{
    @Autowired
    private TWorkInformationService tWorkInformationService;

    @Autowired
    private TWorkInformationDetailService tWorkInformationDetailService;

    @Resource
    private StudentDealRemote studentDealRemote;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //默认获取当前用户的作业
        PageUtils page = tWorkInformationService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        TWorkInformationEntity tWorkInformation = tWorkInformationService.getById(id);

        return R.ok().put("tWorkInformation", tWorkInformation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public  R save(@RequestBody TWorkInformationEntity tWorkInformation) {
        tWorkInformationService.save(tWorkInformation);
        //保存作业信息
        tWorkInformationDetailService.saveRelateDetail(tWorkInformation);
        HashMap<String, String> stringMap = new HashMap<>();
        stringMap.put("studentCode",tWorkInformation.getWorkSubmitStudentCode());
        stringMap.put("studentName",tWorkInformation.getWorkSubmitStudentName());
        stringMap.put("workCode",tWorkInformation.getWorkCode());
        stringMap.put("workName",tWorkInformation.getWorkName());
        stringMap.put("workAuditTeacherName",tWorkInformation.getWorkAuditTeacherName());
        //生产学生作业记录
        studentDealRemote.dealStudentWork(stringMap);
        return  R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public  R update(@RequestBody TWorkInformationEntity tWorkInformation) {
        tWorkInformationService.updateById(tWorkInformation);
        tWorkInformationDetailService.saveRelateDetail(tWorkInformation);
        return  R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public  R delete(@RequestBody Long[] ids) {
        tWorkInformationService.removeByIds(Arrays.asList(ids));
        return  R.ok();
    }
    //做作业
    @RequestMapping("/startHomework/{id}")
    public R startHomework(@PathVariable("id") Long id) throws ParseException {
        TWorkInformationEntity tWorkInformation = tWorkInformationService.getById(id);
        String workTime = tWorkInformation.getWorkSubmitTime();
        if(StringUtils.isNotEmpty(workTime)){
            Date nowdate = new Date();
            Date startdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(workTime);
            if(nowdate.after(startdate)){
                return R.error("已过作业提交时间,不能进行提交作业");
            }
            return R.ok();
        }else{
            return R.error("作业提交时间不能为空");
        }
    }

}
